package com.cci.kangdao.wechatOrder.controller;

import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.wechatOrder.dto.request.ServiceLocationRequest;
import com.cci.kangdao.wechatOrder.exception.NotCheckException;
import com.cci.kangdao.wechatOrder.service.IWechatReloadLocationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 服务站推送短信给用户服务
 * @author: zyf
 * @create: 2021-03-23
 */
@Controller
public class WeChatServiceStationController extends WeChatBaseController {

    @Resource
    private IWechatReloadLocationService locationService;
    @Autowired
    private RedisClientUtils redisClientUtils;

    /**
     * APP-服务商用户发送终端重新获取位置的接口
     *
     * @param phone    请求参数
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getServiceReloadLocationNoWorkOrder.aspx", method = RequestMethod.POST)
    public void getServiceReloadLocationNoWorkOrder(@RequestParam("phone") String phone, HttpServletRequest request,
                                                    HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(phone)) {
                ParameterTool.writeResponse(response, -99, "请输入手机号");
            }
            // 加锁处理，避免发生多次提交。
            String lockKey= "updateAddressBefore-" + phone ;
            if (!redisClientUtils.lock(lockKey, 60L)) {
                //防止并发
                ParameterTool.writeErrorResponse(response, -78, "短信已发送，请1分钟后再试！");
            } else {
                //调用服务
                locationService.getServiceReloadLocationNoWorkOrder(phone, request, response);
            }

        } catch (NotCheckException e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, "发送短信失败");
        }
    }

    /**
     * APP-服务商用户发送终端重新获取位置的接口
     * @param locationRequest 请求参数
     * @param request
     * @param response
     */
    @RequestMapping(value = "/serviceReloadLocation.aspx", method = RequestMethod.POST)
    public void getServiceReloadLocation(ServiceLocationRequest locationRequest, HttpServletRequest request,
                                         HttpServletResponse response) {
        try{
            //调用服务
            locationService.reloadLocationSendSms(locationRequest,request,response);
        }catch (NotCheckException e){
            e.printStackTrace();
            ParameterTool.writeResponse(response,-99,e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            ParameterTool.writeResponse(response,-99,"发送短信失败");
        }
    }

    /**
     *
     * @param locationRequest
     * @param request
     * @param response
     */
    @RequestMapping(value = "/analysisToken.aspx", method = RequestMethod.POST)
    public void analysisToken(ServiceLocationRequest locationRequest, HttpServletRequest request,
                          HttpServletResponse response) {
        try{
            //调用服务
            locationService.analysisToken(locationRequest,request,response);
        }catch (NotCheckException e){
            e.printStackTrace();
            ParameterTool.writeResponse(response,-99,e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            ParameterTool.writeResponse(response,-99,"token解析失败");
        }
    }

    /**
     * 上传位置服务
     * @param locationRequest 位置请求参数
     * @param request
     * @param response
     */
    @RequestMapping(value = "/uploadLocation.aspx", method = RequestMethod.POST)
    public void uploadLocation(ServiceLocationRequest locationRequest, HttpServletRequest request,
                               HttpServletResponse response){
        try{
            //调用服务
            locationService.uploadLocation(locationRequest,request,response);
        } catch (NotCheckException e){
            e.printStackTrace();
            ParameterTool.writeResponse(response,-99,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, "位置更新失败");
        }

    }

    /**
     * 上传位置
     *
     * @param locationRequest 位置请求参数
     * @param request
     * @param response
     */
    @RequestMapping(value = "/setLocationRedis.aspx", method = RequestMethod.POST)
    public void setLocationRedis(ServiceLocationRequest locationRequest, HttpServletRequest request,
                                 HttpServletResponse response) {
        try {
            //调用服务
            locationService.setLocationRedis(locationRequest, request, response);
        } catch (NotCheckException e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, "位置更新失败");
        }

    }

    /**
     * 获取位置   与setLocationRedis    为互生关系
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getLocationRedis.aspx", method = RequestMethod.GET)
    public void getLocationRedis(@RequestParam("phone") String phone, HttpServletRequest request,
                                 HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(phone)) {
                ParameterTool.writeResponse(response, -99, "清输入手机号");
            }
            //调用服务
            locationService.getLocationRedis(phone, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            ParameterTool.writeResponse(response, -99, "位置更新失败");
        }

    }

}
